package com.likou;

import java.util.Arrays;

/**
 * Author: 陈桂明
 * Date: 2024/9/20 23:24
 * Description: Q4
 */
public class Q4 {
    public static void main(String[] args) {
        int[] nums1 = new int[]{1, 3};
        int[] nums2 = new int[]{2};
        Q4 q4 = new Q4();
        double medianSortedArrays = q4.findMedianSortedArrays(nums1, nums2);
        System.out.println(medianSortedArrays);
    }

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        // 合并数组
        int[] merge = new int[nums1.length + nums2.length];
        System.arraycopy(nums1, 0, merge, 0, nums1.length);
        System.arraycopy(nums2, 0, merge, nums1.length, nums2.length);
        // System.out.println(Arrays.toString(merge));

        Arrays.sort(merge); // 对数组进行排序
        // System.out.println(Arrays.toString(merge));

        double rs;
        if (merge.length % 2 == 0) {
            rs = (merge[merge.length / 2 - 1] + merge[merge.length / 2]) / (double) 2;
        } else {
            rs = merge[merge.length / 2];
        }

        return rs;
    }
}
